package org.vsg.common.pagination.web.taglibs;

import java.util.logging.Logger;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

import org.vsg.common.pagination.Pagination;
import org.vsg.common.pagination.keyword.PaginationKeyword;
import org.vsg.common.pagination.sort.SortField;
import org.vsg.common.pagination.wrapper.GenericWebPaginationWrapper;
import org.vsg.common.pagination.wrapper.GenericWebSortByPaginationWrapper;
import org.vsg.common.pagination.wrapper.PaginationWrapper;
import org.vsg.common.pagination.wrapper.SortFieldPaginationWrapper;
import org.vsg.common.pagination.wrapper.WebPaginationWrapper;

/**
 * JSP Tag Handler class
 *
 * @jsp.tag   name = "PageSortby"
 *            display-name = "Name for PageSortby"
 *            description = "Description for PageSortby"
 *            body-content = "empty"
 */
public class PageSortbyTag extends TagSupport {

	private Logger logger = Logger.getLogger(PageSortbyTag.class.toString());
	
	private String sortByField;
	
	private String name;
	
	public String getSortByField() {
		return sortByField;
	}



	/**
	 * @return Returns the name.
	 */
	public String getName() {
		return name;
	}


	/**
	 * @param name The name to set.
	 */
	public void setName(String name) {
		this.name = name;
	}


	public void setSortByField(String sortByField) {
		this.sortByField = sortByField;
	}



	public int doStartTag() throws JspException {
		PaginationBarTag parent = (PaginationBarTag)this.findAncestorWithClass(this,PaginationBarTag.class);
		Object webPaginationObj = null;
		Pagination page  = null;


		if (parent == null) {
			// load web pagination
			webPaginationObj = pageContext.getAttribute(name,PageContext.PAGE_SCOPE);
			if (webPaginationObj == null) {
				webPaginationObj = pageContext.getAttribute(name,PageContext.REQUEST_SCOPE);
			}
			if (webPaginationObj == null) {
				webPaginationObj = pageContext.getAttribute(name,PageContext.SESSION_SCOPE);
			}			
			if (webPaginationObj == null) {
				webPaginationObj = pageContext.getAttribute(name,PageContext.APPLICATION_SCOPE);
			}
			
			if (webPaginationObj instanceof PaginationWrapper) {
				page = ((PaginationWrapper)webPaginationObj).getPagination();
			}
		}
		
		// load the pagination from parent tag
		else {
			webPaginationObj = pageContext.getAttribute(parent.getId());
			page = parent.getPagination();
		}
		
		// parse pagination wrapper
		String uri = null;
		String otherParamsUrl = null;
		StringBuffer url = new StringBuffer();
		
		// defined variable for web pagination wrapper
		if (webPaginationObj instanceof WebPaginationWrapper) {
			WebPaginationWrapper wrapper = (WebPaginationWrapper)webPaginationObj;
			uri = wrapper.getUrl();
			otherParamsUrl = wrapper.getOtherParamsUrl();
		}
	

		// defined variable for sort field wrapper
		if (webPaginationObj instanceof SortFieldPaginationWrapper) {
			SortFieldPaginationWrapper wrapper = (SortFieldPaginationWrapper)webPaginationObj;
			
			SortField sortField = wrapper.getSortField();
						
			if (uri!=null) {
				if (uri.endsWith("?")) {
					url.append(uri);
				} else {
					url.append(uri+"?");
				}
			}
			
			if (otherParamsUrl != null) {
				url.append(otherParamsUrl);
			}

			String ascSortByUrl = url.toString()+"&"+PaginationKeyword.SINGLESORTBY_FIELD+"="+sortByField+" "+SortField.ASCENDING;
			String descSortByUrl = url.toString()+"&"+PaginationKeyword.SINGLESORTBY_FIELD+"="+sortByField+" "+SortField.DESCENDING;
			
			
		
			if (sortField != null) {
				System.out.println("current : " + sortByField);
				System.out.println("asc url : " + ascSortByUrl);
				System.out.println("desc url : " + descSortByUrl);
				
				if (sortByField.equals(sortField.getField())) {
					System.out.println(sortField.getField());

					//pageContext.setAttribute("isCurrentSort" ,new Boolean(true));
										
					if (sortField.getSortOrder().equals(SortField.ASCENDING)) {
						pageContext.setAttribute("nextSortByUrl",descSortByUrl);
						pageContext.setAttribute("nextSortbyState",SortByState.DESC);
						pageContext.setAttribute("currentSortByState" ,SortByState.ASC);	
					
					} else if (sortField.getSortOrder().equals(SortField.DESCENDING)) {
						pageContext.setAttribute("nextSortByUrl",ascSortByUrl);
						pageContext.setAttribute("currentSortByState" ,SortByState.DESC);						
						pageContext.setAttribute("nextSortbyState",SortByState.ASC);
					}
				} 
				
				// sort field is not equal for another state
				else {
					pageContext.setAttribute("nextSortByUrl",ascSortByUrl);
					pageContext.setAttribute("currentSortByState" ,SortByState.EMPTY);						
					pageContext.setAttribute("nextSortbyState",SortByState.ASC);					

				}
			} else {
				// define sortfield value

				pageContext.setAttribute("nextSortByUrl",ascSortByUrl);
				pageContext.setAttribute("currentSortByState" ,SortByState.EMPTY);
				pageContext.setAttribute("nextSortbyState",SortByState.ASC);
			}			
			
		}
		
		// if not support sort by wrapper
		else {
			pageContext.setAttribute("isAscSortBy",new Boolean(false));						
			pageContext.setAttribute("isDescSortBy",new Boolean(false));
			pageContext.setAttribute("ascSortByUrl","#");
			pageContext.setAttribute("descSortByUrl","#");
			logger.info("WebPaginationWrapper is not supported by 'sortby'");			
		}

		// change sort by pagnaintion

		return Tag.EVAL_BODY_INCLUDE;
	}


}
